﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Repository.ViewModels;
using FRL.DebtChased.Website;

namespace WebApplication.Finance.Controls
{

	/// <summary>
	/// Declaration of ClientWorkflowAssignmentCtrl class
	/// </summary>
	public partial class ClientWorkflowAssignmentCtrl : System.Web.UI.UserControl
	{

		/////////////////////////////////////////////////////////////
		// Constants

		private const string ClientIdTag = "ClientRecordId";

		/////////////////////////////////////////////////////////////
		// Attributes

		public int ClientRecordID
		{
			get 
			{
				int? iValue = ViewState[ClientIdTag] as int?;
#if DEBUG
				Debug.Assert(iValue != null);
#endif // #if DEBUG
				return iValue.Value;
			}
			set 
			{
				ViewState[ClientIdTag] = value; 
			}
		}

		private Dictionary<int, string> m_workflowDict;

		/////////////////////////////////////////////////////////////
		// Event handlers

		protected void Page_Load(object sender, EventArgs e)
		{

			// If this is a postback...
			if (this.IsPostBack)
			{

			}
			else
			{

				// Declare an entities object
				using (DebtChasedEntities ent = new DebtChasedEntities())
				{

					// Create a dictionary of workflows
					m_workflowDict = Workflow.GetDictionary(ent, true, Definitions.Default);

					// Load the workflow list model
					ClientWorkflowAssignmentsModel m = new ClientWorkflowAssignmentsModel(ent, ClientRecordID);
					gridAssignments.DataSource = m.Elements;
					gridAssignments.DataBind();

				}

				// Set the grid border
				gridAssignments.RowStyle.BorderColor = MyApplication.BindMyApplication().SecondaryWebColor;

			}

		}

		protected void gridAssignments_RowDataBound(object sender, GridViewRowEventArgs e)
		{

			// If this is a data-row...
			if (e.Row.RowType == DataControlRowType.DataRow)
			{

				// Get the model element
				ClientWorkflowAssignmentsModel.Element el = e.Row.DataItem as ClientWorkflowAssignmentsModel.Element;

				// Load the workflow drop-down
				DropDownList cboWorkflow = e.Row.FindControl("cboWorkflow") as DropDownList;
				cboWorkflow.BindToDictionary(m_workflowDict);
				cboWorkflow.SelectedValue = el.WorkflowId.ToString();

			}

		}

		/////////////////////////////////////////////////////////////
		// Operations

		public Dictionary<int, int> GetAssignmentMap()
		{

			// Build a map of status and workflow id
			Dictionary<int, int> Ftmp = new Dictionary<int, int>();

			// Loop through the rows
			for (int iRowIndex = 0; iRowIndex < gridAssignments.Rows.Count; iRowIndex++)
			{

				// Get the status
				int iInvoiceStatusId = int.Parse(gridAssignments.DataKeys[iRowIndex].Value.ToString());

				// Get the selected workflow id
				DropDownList cboWorkflow = gridAssignments.Rows[iRowIndex].FindControl("cboWorkflow") as DropDownList;
				string strValue = cboWorkflow.SelectedValue;
				int iWorkflowId = int.Parse(strValue);

				// If there is a workflow...
				if (iWorkflowId != 0)
					Ftmp.Add(iInvoiceStatusId, iWorkflowId);

			}

			// Return varaible
			return Ftmp;

		}

	}

}